﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MYTech.Data.Oracle;
using MYTech.Oracle;
using MYTech.Utils;

namespace MYTech.Data.Oracle
{
    public partial class SetConnectForm : Form
    {
        public SetConnectForm()
        {
            InitializeComponent();
        }

        private void SetConnectForm_Load(object sender, EventArgs e)
        {
            Connect conn = LoadConfig.Connection;
            if (conn != null)
            {
                tServerIP.Text = conn.ServerIP;
                tPort.Value = conn.ServerPort;
                tDataBase.Text = conn.ServerName;
                tUserName.Text = conn.UserName;
                tPassword.Text = conn.Password;
            }
        }

        private bool isSaving = false;
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (!backgroundWorkerMain.IsBusy)
            {
                isSaving = true;
                LoadConfig.SetOracleConfig(tServerIP.Text, tDataBase.Text, Convert.ToInt32(tPort.Value), tUserName.Text, tPassword.Text);
                MYTech.Utils.Utils.RunBackgroundWorker(backgroundWorkerMain, backgroundWorkerMain_DoWork, backgroundWorkerMain_RunWorkerCompleted);
            }
            else
                MessageBox.Show("正忙...", "提示");
        }

        private void backgroundWorkerMain_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                if (isSaving)
                    LoadConfig.SaveOracleConfig();
                else
                    e.Result = DataHelper.Test();
            }
            catch (Exception ex)
            {
                e.Result = ex.Message;
            }
        }

        private void backgroundWorkerMain_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Result == null)
            {
                if (isSaving)
                    MessageBox.Show("保存成功...", "提示");
                else
                    MessageBox.Show("连接成功...", "提示");
            }
            else
                MessageBox.Show(string.Format("{0}", e.Result), "提示");
        }

        private void TextValidate()
        {
            if (!RegexHelper.Validate(tServerIP.Text, @"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$") || string.IsNullOrEmpty(tPort.Text) || string.IsNullOrEmpty(tDataBase.Text) || string.IsNullOrEmpty(tUserName.Text) || string.IsNullOrEmpty(tPassword.Text))
            {
                btnTest.Enabled = false;
                btnOK.Enabled = false;
            }
            else
            {
                btnTest.Enabled = true;
                btnOK.Enabled = true;
            }
        }

        private void TextChanged(object sender, EventArgs e)
        {
            TextValidate();
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            if (!backgroundWorkerMain.IsBusy)
            {
                isSaving = false;
                LoadConfig.SetOracleConfig(tServerIP.Text, tDataBase.Text, Convert.ToInt32(tPort.Value), tUserName.Text, tPassword.Text);
                MYTech.Utils.Utils.RunBackgroundWorker(backgroundWorkerMain, backgroundWorkerMain_DoWork, backgroundWorkerMain_RunWorkerCompleted);
            }
            else
                MessageBox.Show("正忙...", "提示");
        }
    }
}
